<%--
  Created by IntelliJ IDEA.
  User: apink
  Date: 2019/12/16
  Time: 22:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=UTF-8">
    <title>用户管理</title>
    <script src="https://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
    <div style="text-align: center;">
        <h2>简易登陆页</h2></br>
        用户：<input type="text" id="username"><br>
        密码：<input type="password" id="password"><br><br>
        <button id="login">登录</button>
        <p id="cookieVal"></p>
    </div>

    <script>
        const _pathName = window.document.location.pathname;
        const _projectName = _pathName.substring(0,_pathName.substr(1).indexOf('/')+1);

        $("#login").click(function(){
            var inputData = {};
            inputData.username = $("#username").val();
            inputData.password = $("#password").val();
            if(!inputData.username || !inputData.password){
                alert("用户或密码不能为空！");
                return;
            }
            $.ajax({
                method: "post",
                url: _projectName + "/login/login",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(inputData),
                dataType: "json",
                success:function(result){
                    if (result.code == 200) {
                        window.location.href = _projectName + "/index/toIndex";
                        localStorage.setItem("token", result.data);
                    }else {
                        alert(result.message);
                    }
                }});
        });
        function showCookie() {
            var cookieVal = document.cookie;
            $("#cookieVal").html(cookieVal);
        }
        showCookie();
    </script>
</body>
</html>
